import {SET_GOODS,RESET,ADD_CAR,CLEAR_MY_CAR,DEL_THIS,CHANGE_THIS} from "../constant";  //引入定义常量
//ajax请求，获得当前用户的购物车列表
import {getGoods,addGoods,clearCar,delGoods,changeNum} from "../../network/car";

//从数据库获得购物车信息并存储到redux中
export const setGood = (userid) => {
    return async dispatch => {
        let result = await getGoods(userid)
        if(result.code === 200 || result.code === 201){
            dispatch({
                type: SET_GOODS,
                data: result.data
            })
        }
    }
}

//用户添加商品进购物车
export const addCar = (paramsObj) => {
    return async dispatch => {
        let result = await addGoods(paramsObj)
        if(result.code === 200 || result.code === 201){
            dispatch({
                type: ADD_CAR,
                data: result.data
            })
        }
    }
}

//清空购物车
export const clearMyCar = (userid) => {
    return async dispatch => {
        let result = await clearCar(userid)
        if(result.code === 200 || result.code === 201){
            dispatch({
                type: CLEAR_MY_CAR,
                data: result.data
            })
        }
    }
}

//删除整类商品
export const delThis = (userid,gid,gcolor) => {
    return async dispatch => {
        let result = await delGoods(userid,gid,gcolor)
        if(result.code === 200 || result.code === 201){
            dispatch({
                type: DEL_THIS,
                data: result.data
            })
        }
    }
}

//更改商品数量
export const changeThis = (userid,gid,gcolor,type) => {
    return async dispatch => {
        let result = await changeNum(userid,gid,gcolor,type)
        if(result.code === 200 || result.code === 201){
            dispatch({
                type: CHANGE_THIS,
                data: result.data
            })
        }
    }
}
//重置redux
export const reset = () => ({type: RESET,data: []})